﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace Needle.Utils.Parser
{
    /// <summary>
    /// Parses a string into a Linq Expression tree
    /// </summary>
    public class ExpressionParser
    {
        /// <summary></summary>
        public static Predicate<T> ParseAsPredicate<T>(string expression)
        {
            ExpressionParser parser = new ExpressionParser(expression);
            Expression<Predicate<T>> predicateExpression = parser.ParsePredicate<T>();
            return new Predicate<T>(predicateExpression.Compile());
        }
        /// <summary></summary>
        public ExpressionParser(string expression)
        {
            if (String.IsNullOrEmpty(expression))
                throw new ArgumentException("expression is null or empty.", "expression");
            _Expression = expression;
        }

        private string _Expression;

        Dictionary<string, PropertyInfo> symbolTable;
        ParameterExpression expressionParameter;

        private Expression<Predicate<T>> ParsePredicate<T >()
        {
            symbolTable = (from propInfo in typeof(T).GetProperties()
                           select propInfo).ToDictionary(x => x.Name);
            expressionParameter = Expression.Parameter(typeof(T), "item");
            var tokens = new CSharpTokenizer(_Expression)
                .Excluding(
                TokenType.EOF,
                TokenType.NewLine,
                TokenType.None,
                TokenType.WhiteSpace);

            //   Expression expr = null;

            /* implement expression parser here */
            throw new NotImplementedException();
            
          //  return Expression.Lambda<Predicate<T>>(expr, expressionParameter);
        }

    }
}
